package banmuoi.servlet;

import java.util.List;

import banmuoi.entity.Admin;
import banmuoi.entity.Category;
import banmuoi.entity.Permission;
import banmuoi.entity.Product;
import banmuoi.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class TestDB{
	
		static Session hs = HibernateUtil.openSession();
		
		//static String hqlCategory = "FROM Category";
		static String hqlAdmin = "FROM Admin";
		static Query qAdmin = hs.createQuery(hqlAdmin);
		static List<Admin> admins = qAdmin.list();
		
		static String hqlPermission = "FROM Permission";
		static Query qP = hs.createQuery(hqlPermission);
		static List<Permission> pers = qP.list();
		
		
		public static void main(String[] args) {
			for(Admin admin:admins){
				String name = admin.getName();
				String permission = admin.getPermission().getRole();
				System.out.println(name+permission);
			}
			
			/*for(Permission per:pers){
				String role = per.getRole();
				System.out.println(role);
			}*/
			
		}
		
		static void addProduct(){
			Category cate = new Category();
			cate.setId(2);
			
			Product entity = new Product();	
			
			entity.setName("SP2");
			entity.setImage("123.jpg");
			entity.setUnitPrice((double) 12000);
			entity.setQuantity(5);
			entity.setUnitBrief("cái");
			entity.setNote("note");
			entity.setState(true);
			entity.setCategory(cate);
			
			
			Transaction trans = hs.beginTransaction();
			try {
				
				hs.save(entity);
				trans.commit();
				System.out.println("Thành công");
			} catch (HibernateException  e) {
				trans.rollback();
				e.printStackTrace();
				//System.out.println(e.getMessage());
			}finally {
		         hs.close(); 
			}
		}
}
 